package com.qys.livesMall.shop.event.handle.service;

import com.qys.livesMall.shop.event.handle.BaseHandler;
import com.qys.livesMall.shop.event.shop.pojo.ShopStocksPojo;
import com.qys.livesMall.shop.mapper.ShopGoodsSkuStocksMapper;
import com.qys.livesMall.shop.modal.entity.*;
import com.qys.livesMall.shop.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Map;


/**
 * @menu ShopOrderServer
 * @date 2023/11/06 14:37
 */
@Slf4j
@Service
public class ShopStocksServer implements BaseHandler<ShopStocksPojo> {

    @Autowired
    private H2ShopStatisticsInfoService shopStatisticsInfoService;


    @Autowired
    private ShopGoodsSkuStocksMapper skuStocksMapper;


    @Override
    public void correct(Long shopId) {
    }

    @Override
    public void handle(ShopEventInfo<ShopStocksPojo> info) {
        H2ShopStatisticsInfo shopStatisticsInfo=shopStatisticsInfoService.getField(info.getShopId(),H2ShopStatisticsInfo::getId);
        //库存变动，直接查询实时数据，订单退回时不减下单量，数据无法对其
        //库存量 库存额
        Map<String,Object> stock= skuStocksMapper.getStockNumAndAmount(info.getShopId());
        if (stock!=null){
            shopStatisticsInfo.setStocksNum(stock.get("num")!=null?Integer.valueOf(stock.get("num").toString()):0);
            shopStatisticsInfo.setStocksAmount(stock.get("amount")!=null?(BigDecimal) stock.get("amount"):BigDecimal.ZERO);
        }
        shopStatisticsInfoService.updateById(shopStatisticsInfo);
    }

}

